/*
 * @lc app=leetcode.cn id=35 lang=javascript
 *
 * [35] 搜索插入位置
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
// var searchInsert = function(nums, target) {
//   let head = 0, tail = nums.length, mid;
//   while(head < tail){
//     mid = head + ((tail - head) >> 1);
//     if(nums[mid] < target) {
//       head = mid + 1;
//     } else { 
//       tail = mid;
//     }
//   }
//   return head;
// };

/**
 * 复习 2021-08-04
 * 
 * 二分查找
 */
var searchInsert = function(nums, target) {
  let head = 0, tail = nums.length;
  while(head < tail){
    let mid = head + ((tail - head) >> 1);
    if(target === nums[mid]) return mid;
    if(target > nums[mid]) {
      head = mid + 1
    } else {
      tail = mid;
    }
  }
  return head;
}

// @lc code=end

